package co.yixiang.modules.product.rest; /**
 * Copyright (C) 2018-2020
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
import java.util.Arrays;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.aop.ForbidSubmit;
import co.yixiang.modules.product.domain.YxBusinessType;
import co.yixiang.modules.product.service.YxBusinessTypeService;
import co.yixiang.modules.product.service.dto.YxBusinessTypeDto;
import co.yixiang.modules.product.service.dto.YxBusinessTypeQueryCriteria;
import lombok.AllArgsConstructor;
import co.yixiang.logging.aop.log.Log;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import co.yixiang.domain.PageResult;
/**
 * @author lsh
 * @date 2021-06-21
 */
@AllArgsConstructor
@Api(tags = "YxBusinessType管理")
@RestController
@RequestMapping("/api")
public class YxBusinessTypeController {

    private final YxBusinessTypeService yxBusinessTypeService;
    private final IGenerator generator;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/yxBusinessType/download")
    @PreAuthorize("@el.check('admin','yxBusinessType:list')")
    public void download(HttpServletResponse response, YxBusinessTypeQueryCriteria criteria) throws IOException {
        yxBusinessTypeService.download(generator.convert(yxBusinessTypeService.queryAll(criteria), YxBusinessTypeDto.class), response);
    }

    @GetMapping(value="/yxBusinessType")
    @Log("查询YxBusinessType")
    @ApiOperation("查询YxBusinessType")
    /*@PreAuthorize("@el.check('admin','yxBusinessType:list')")*/
    @PreAuthorize("hasAnyRole('admin','YXBUSINESSTYPE_ALL','YXBUSINESSTYPE_SELECT')")
    public ResponseEntity<PageResult<YxBusinessTypeDto>> getYxBusinessTypes(YxBusinessTypeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxBusinessTypeService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping(value="/yxBusinessType")
    @Log("新增YxBusinessType")
    @ApiOperation("新增YxBusinessType")
    @PreAuthorize("hasAnyRole('admin','YXBUSINESSTYPE_ALL','YXBUSINESSTYPE_CREATE')")
    public ResponseEntity<Object> create(@Validated @RequestBody YxBusinessType resources){
        return new ResponseEntity<>(yxBusinessTypeService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping(value="/yxBusinessType")
    @Log("修改YxBusinessType")
    @ApiOperation("修改YxBusinessType")
    @PreAuthorize("hasAnyRole('admin','YXBUSINESSTYPE_ALL','YXBUSINESSTYPE_EDIT')")
    public ResponseEntity<Object> update(@Validated @RequestBody YxBusinessType resources){
        yxBusinessTypeService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ForbidSubmit
    @Log("删除YxBusinessType")
    @ApiOperation("删除YxBusinessType")
    @PreAuthorize("hasAnyRole('admin','YXBUSINESSTYPE_ALL','YXBUSINESSTYPE_DELETE')")
    @DeleteMapping(value="/yxBusinessType/{id}")
    public ResponseEntity<Object> delete(@PathVariable Integer id) {
            yxBusinessTypeService.removeById(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
